<template >
  <div class="ordermanage-container">
    <breadcrumb style="position: absolute;width:100%;"></breadcrumb>
    <div class="content">
      <div class="query">
        <div class="margin_right_20">
          订单编号:&nbsp;
          <el-input
            v-model="orderNo"
            placeholder="请输入订单编号"
            style="width:200px;height:30px;"
            @keyup.enter.native="get_orderList(1)"
          ></el-input>
        </div>
        <div class="margin_right_20">
          用户ID:&nbsp;
          <el-input
            v-model="userNo"
            placeholder="请输入用户ID"
            style="width:100px;height:30px;"
            @keyup.enter.native="get_orderList(1)"
          ></el-input>
        </div>
        <div class="margin_right_20">
          商品名称:&nbsp;
          <el-input
            v-model="goodsName"
            placeholder="请输入商品名称"
            style="width:180px;height:30px;"
            @keyup.enter.native="get_orderList(1)"
          ></el-input>
        </div>
        <div class="margin_right_20">
          订单所属活动:&nbsp;
          <el-select v-model="positionId" style="width: 130px;">
            <el-option
              v-for="item in options1"
              :key="item.id"
              :label="item.position"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>
        <div class="margin_right_20">
          订单状态:&nbsp;
          <el-select v-model="orderStatus" style="width: 110px;">
            <el-option
              v-for="item in options2"
              :key="item.value2"
              :label="item.label"
              :value="item.value2"
            ></el-option>
          </el-select>
        </div>
        <div class="btn">
          <el-button type="primary" icon="el-icon-search" @click="get_orderList(1)">查询</el-button>
          <el-button
            v-if="orderNo!=''||userNo!=''||goodsName!=''||positionId!=''||orderStatus!=''||startTime!=''||endTime!=''"
            type="danger"
            style="margin-right:100px"
            icon="el-icon-upload2"
            @click="exportExcel"
          >导出</el-button>
        </div>
      </div>

      <div class="query">
        <div class="start_time margin_right_20">
          创建时间(开始):&nbsp;
          <div class="block">
            <el-date-picker
              type="datetime"
              v-model="startTime"
              placeholder="选择日期时间"
              style="width:200px"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
        </div>
        <div class="end_time margin_right_20">
          创建时间(结束):&nbsp;
          <div class="block">
            <el-date-picker
              type="datetime"
              placeholder="选择日期时间"
              style="width:200px"
              v-model="endTime"
              default-time="23:59:59"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
        </div>
      </div>
      <div class="table">
        <el-table :data="orderList" style="width: 100%" height="548" id="table1">
          <el-table-column prop="orderNo" label="订单编号" width="220px"></el-table-column>
          <el-table-column prop="userNo" label="用户ID" width="130px"></el-table-column>
          <el-table-column prop="userName" label="用户昵称"></el-table-column>
          <el-table-column prop="goodPosition" label="订单所属活动"></el-table-column>
          <el-table-column prop="goodsName" label="商品名称" width="400px"></el-table-column>
          <el-table-column prop="amount" label="订单金额"></el-table-column>
          <el-table-column prop="status" label="订单状态"></el-table-column>
          <el-table-column prop="createtime" label="创建时间" width="200"></el-table-column>
          <el-table-column label="操作" width="130">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="sent_(scope.row.id)"
                v-if="scope.row.status=='待发货'"
              >发货</el-button>
              <el-button
                type="text"
                size="small"
                @click="jump_order_modify(scope.row.id,scope.row.orderStatus)"
              >修改</el-button>
              <el-button type="text" size="small" @click="jump_order_detail(scope.row.id)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :currentPage="currentPage"
          :page="page"
          :total="total"
          :pageSize="pageSize"
          @change-current="currentChange"
          @change-size="sizeChange"
        ></pagination>
        <el-dialog :visible.sync="dialogVisible" width="20%">
          <el-form label="form" v-model="form">
            <el-form-item label="快递公司：" label-width="110px">
              <el-input v-model="form.orderCourier"></el-input>
            </el-form-item>
            <el-form-item label="快递单号：" label-width="110px">
              <el-input v-model="form.orderLogisticNo"></el-input>
            </el-form-item>
            <el-form-item label="快递公司编码：" label-width="110px">
              <el-input v-model="form.orderCourierCode"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <div class="btn">
              <el-button @click="dialogVisible=false" size="small" style="width:140px">取 消</el-button>
              <el-button type="primary" @click="deliverGoods" size="small" style="width:140px">确 定</el-button>
            </div>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import breadcrumb from "../../components/breadcrumb";
import pagination from "../../components/page";
import { mapActions } from "vuex";
export default {
  components: { breadcrumb, pagination },
  data() {
    return {
      form: {},
      currentPage: 1,
      pageSize: 10,
      page: 1,
      total: 0,
      options1: [],
      dialogVisible: false,
      dialogVisible1: false,
      columnNum: "",
      options2: [
        { value2: "", label: "全部" },
        { value2: "1", label: "待付款" },
        { value2: "2", label: "未填地址" },
        { value2: "3", label: "待发货" },
        { value2: "4", label: "待收货" },
        { value2: "5", label: "待评价" },
        { value2: "6", label: "已完成" },
        { value2: "7", label: "待分享" },
        { value2: "8", label: "待领取" },
        { value2: "9", label: "售后" },
        { value2: "10", label: "已关闭" }
      ],
      id: "",
      orderList: [],
      orderNo: "",
      userNo: this.$route.query.ids || "", //获取从人员管理详情的订单总数传过来的ids值
      goodsName: "",
      positionId: "",
      orderStatus: "",
      startTime: "",
      endTime: "",
      downList: {},
      Address: ""
    };
  },
  created() {
    if (localStorage.getItem("pageMsg")) {
      let pageMsg = JSON.parse(localStorage.getItem("pageMsg"));
      this.currentPage = pageMsg.currentPage;
      this.pageSize = pageMsg.pageSize;
      this.orderNo = pageMsg.orderNo;
      this.userAddress = pageMsg.userAddress;
      this.goodsName = pageMsg.goodsName;
      this.positionId = pageMsg.positionId;
      this.orderStatus = pageMsg.orderStatus;
      this.startTime = pageMsg.startTime;
      this.endTime = pageMsg.endTime;
      localStorage.removeItem("pageMsg");
    }
    this.get_orderList();
    // 获取商品类型
    this.positions();
  },
  methods: {
    positions() {
      this.$axios.post("/web_goods_pos/list").then(res => {
        this.options1 = res.data.data;
        this.options1.unshift({ id: "", position: "全部" });
      });
    },
    sizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.get_orderList();
    },
    currentChange(val) {
      this.currentPage = val;
      this.get_orderList();
    },
    sent_(id) {
      this.dialogVisible = true;
      this.id = id;
    },
    deliverGoods() {
      if (!this.form.orderCourier) {
        return this.$message({
          type: "warning",
          message: "请输入快递公司"
        });
      }
      if (!this.form.orderLogisticNo) {
        return this.$message({
          type: "warning",
          message: "请输入快递单号"
        });
      }
      if (!this.form.orderCourierCode) {
        return this.$message({
          type: "warning",
          message: "请输入快递公司编码"
        });
      }
      let data = {
        id: this.id,
        orderCourier: this.form.orderCourier,
        orderCourierCode: this.form.orderCourierCode,
        orderLogisticNo: this.form.orderLogisticNo
      };
      this.$axios.post("/web_order/shipped", data).then(result => {
        if (result.data.status == 0) {
          if (result.data.data == true) {
            this.get_orderList();
            return this.$message({
              type: "success",
              message: "操作成功"
            });
          } else {
            return this.$message({
              type: "warning",
              message: "操作失败"
            });
          }
        }
      });
      this.dialogVisible = false;
    },

    exportExcel() {
      this.$confirm("此操作将导出表格是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        var data =
          "orderNo=" +
          this.orderNo +
          "&userNo=" +
          this.userNo +
          "&goodsName=" +
          this.goodsName +
          "&positionId=" +
          this.positionId +
          "&orderStatus=" +
          this.orderStatus +
          "&startTime=" +
          this.startTime +
          "&endTime=" +
          this.endTime;
        this.$axios
          .post("/web_order/page/export", data)
          .then(result => {
            if (result.data.data) {
              let data = result.data.data;
              this.downList = data;
              this.downList.forEach(item => {
                item.status = this.options2[parseInt(item.status)].label;
                item.Address =
                  item.userAddressProvince +
                  item.userAddressRegion +
                  item.userAddressTown +
                  item.userAddress;
                item.orderNo = item.orderNo + "\t";
                item.userAddressPhone = item.userAddressPhone + "\t";
              });
            } else {
              this.downList = [];
            }
            import("@/vendor/Export2Excel").then(excel => {
              const tHeader = [
                "用户ID",
                "用户昵称",
                "订单ID",
                "商品名称A",
                "商品A购买数量",
                "商品A成本价",
                "商品名称B",
                "商品B购买数量",
                "商品B成本价",
                "收货姓名",
                "收货电话",
                "收货地址",
                "订单状态"
              ];
              //表格第一行标题
              const filterVal = [
                "userNo",
                "userName",
                "orderNo",
                "goodsName",
                "goodsNumber",
                "goodsCost",
                "goodsBName",
                "goodsBNumber",
                "goodsBCost",
                "userAddressName",
                "userAddressPhone",
                "Address",
                "status"
              ];
              //表格跟接口字段对应
              const list = this.downList;
              const data = this.formatJson(filterVal, list); //数据过滤
              excel.export_json_to_excel({
                header: tHeader, //表头 必填
                data, //具体数据 必填
                filename: "order-list", //文件名 非必填
                autoWidth: true, //单元格是否要自适应宽度  非必填
                bookType: "csv" //非必填
              });
              this.downloadLoading = false;
            });
          })
          .catch(() => {
            this.downloadLoading = false;
          });
      });
    },
    // 过滤数据方法，例如时间格式
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === "creatDate") {
            var date = new Date(v[j]);
            return formatTime(date, "yyyy-MM-dd hh:mm:ss");
          } else {
            return v[j];
          }
        })
      );
    },
    jump_order_detail(id) {
      localStorage.setItem("path", JSON.stringify(this.$route.path));
      localStorage.setItem(
        "pageMsg",
        JSON.stringify({
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          orderNo: this.orderNo,
          userAddress: this.userAddress,
          goodsName: this.goodsName,
          positionId: this.positionId,
          orderStatus: this.orderStatus,
          startTime: this.startTime,
          endTime: this.endTime
        })
      );

      this.$router.push("/orderDetail?id=" + id);
    },
    jump_order_modify(id, status) {
      localStorage.setItem("path", JSON.stringify(this.$route.path));
      localStorage.setItem(
        "pageMsg",
        JSON.stringify({
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          orderNo: this.orderNo,
          userAddress: this.userAddress,
          goodsName: this.goodsName,
          positionId: this.positionId,
          orderStatus: this.orderStatus,
          startTime: this.startTime,
          endTime: this.endTime
        })
      );
      this.$router.push("/orderModify?id=" + id);
    },
    get_orderList() {
      var data =
        "current=" +
        this.currentPage +
        "&size=" +
        this.pageSize +
        "&orderNo=" +
        this.orderNo +
        "&userNo=" +
        this.userNo +
        "&goodsName=" +
        this.goodsName +
        "&positionId=" +
        this.positionId +
        "&orderStatus=" +
        this.orderStatus +
        "&startTime=" +
        this.startTime +
        "&endTime=" +
        this.endTime;
      this.$axios.post("/web_order/page", data).then(result => {
        if (result.data.status == 0) {
          this.total = result.data.data.total;
          this.page = result.data.data.pages;
          this.orderList = result.data.data.records;
          this.orderList.forEach(item => {
            item.status = this.options2[parseInt(item.status)].label;
          });
        }
      });
    },

    // 校验结束时间
    verfy_startTime() {
      if (this.startTime) {
        this.timeCompare(1);
      }
    },
    //   校验结束时间
    verfy_endtime() {
      if (this.endTime) {
        this.timeCompare(2);
      }
    },

    timeCompare(flag) {
      //  flag  用于标记是清楚开始时间，还是清楚结束时间
      if (
        new Date(this.startTime).getTime() >= new Date(this.endTime).getTime()
      ) {
        if (flag == 1) {
          this.startTime = "";
        } else {
          this.endTime = "";
        }
        return this.$message({
          type: "warning",
          message: "'拼团创建时间-开始'必须小于'拼团创建时间-结束'"
        });
      }
    }
  }
};
</script>

<style lang="less" >
.ordermanage-container {
  @import "../../style/table.less";
  @import "../../style/content.less";
  display: flex;
  flex-flow: column;
  position: relative;
  height: 100%;
  .el-table .cell {
    white-space: pre-line !important;
  }
  .el-table__row > td {
    height: 50px !important;
  }
  .el-table .cell {
    white-space: pre-line !important;
  }
  .content {
    .query {
      .start_time {
        display: flex;
        align-items: center;
      }
      .end_time {
        display: flex;
        align-items: center;
      }
      .btn {
        .el-button {
          width: 70px;
        }
      }
    }
  }
}
</style>

